package com.longteng.diamond.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.longteng.diamond.dao.TestCaseMapper;
import com.longteng.diamond.domain.InterfaceTest;
import com.longteng.diamond.domain.ProjectShareNode;
import com.longteng.diamond.domain.TestCase;
import com.longteng.diamond.po.TablePage;
import com.longteng.diamond.po.TreeMenu;
import com.longteng.diamond.util.ReplaceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import sun.reflect.generics.tree.Tree;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator
 * DATE : 2018/6/28
 * TIME : 20:33
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Slf4j
@Component
public class TestCaseService {
    @Autowired
    TestCaseMapper testCaseMapper;
    @Autowired
    InterfaceTestService interfaceTestService;
    @Autowired
    ReplaceUtil replaceUtil;

    /**
     * 获取用例
     * @param testCase
     * @return interfaceTest
     */
    public TablePage getTestCase(TestCase testCase) {
        TablePage tablePage = new TablePage();
        try {
            List<TestCase> interfaceTestList = testCaseMapper.getTestCase(testCase);
            tablePage.setRows(interfaceTestList);
            long total = testCaseMapper.queryAllRow(testCase);
            tablePage.setTotal(total);
        } catch (Exception e) {
            log.error("查询接口列表异常", e);
        }
        return tablePage;
    }

    /**
     * 新增测试用例
     *
     * @param testCase
     * @return void
     */
    public void addTestCase(TestCase testCase) {
        String expectResult =testCase.getExpectResult();
        expectResult=replaceUtil.remove(expectResult);
        testCase.setExpectResult(expectResult);
        testCaseMapper.addTestCase(testCase);
    }

    /**
     * 修改测试用例
     *
     * @param testCase
     * @return void
     */
    public void updateTestCase(TestCase testCase) {
        String expectResult =testCase.getExpectResult();
        expectResult=replaceUtil.remove(expectResult);
        testCase.setExpectResult(expectResult);
        testCaseMapper.updateTestCase(testCase);
    }

    /**
     * 根据id获取测试用例
     *
     * @param interfaceId
     * @return TestCase
     */
    public TestCase findTestCaseById(Integer interfaceId) {
        TestCase testCase =testCaseMapper.findTestCaseById(interfaceId);
        return testCase;
    }

    /**
     * 删除用例
     *
     * @param interfaceId
     * @return void
     */
    public void deleteTestCaseById(Integer interfaceId) {
        testCaseMapper.deleteTestCaseById(interfaceId);
    }

    /**
     * 绑定接口
     *
     * @param interfaceId
     * @param caseIds
     * @return void
     */
    public void bindInterface(Integer interfaceId, Integer[] caseIds) {
        Map map = Maps.newHashMap();
        map.put("interfaceId", interfaceId);
        map.put("caseIds", caseIds);
        testCaseMapper.bindInterface(map);
    }

    /**
     * 按照模块获取测试用例
     *
     * @param projectId
     * @return void
     * http://127.0.0.1:8881/testCaseController/findTestCaseGroupModule?projectId=7
     */
    public List<TreeMenu> findTestCaseGroupInterface(Integer projectId) {
        List<InterfaceTest> interfaceTestList = interfaceTestService.findInterfaceAndTestCase(projectId);
        List<TreeMenu> treeMenuList = Lists.newArrayList();
        for (InterfaceTest interfaceTest : interfaceTestList) {
            List<TestCase> testCaseList=interfaceTest.getTestCaseList();
            if(CollectionUtils.isEmpty(testCaseList)){
                continue;
            }
            TreeMenu treeMenu = new TreeMenu();
            treeMenu.setName(interfaceTest.getInterfaceAliasName());
            treeMenu.setId(interfaceTest.getInterfaceId());
            List<TreeMenu> caseTreeList=Lists.newArrayList();
            for(TestCase testCase:testCaseList){
                TreeMenu caseTree = new TreeMenu();
                caseTree.setName(testCase.getTestCaseName());
                caseTree.setId(testCase.getTestCaseId());
                caseTreeList.add(caseTree);
            }
            treeMenu.setChildren(caseTreeList);
            treeMenuList.add(treeMenu);
        }
        return treeMenuList;
    }
}
